import {
    SET_RESULT
} from '../mutation-types';
import api from '../../api';

const state = {
    result: [],
    message: [],
}

function timeDate(date) {
    if (typeof(date) != 'string') {
        return (date.time > 60 ? parseInt(date.time / 60) : 1)
    } else {
        return 0;
    }
}
const mutations = {
        [SET_RESULT](state, data) {
            state.result = data;
            state.message = data.message;
            // if (data.result != null) {
            state.result.time = timeDate(data.time);
            state.result.kcal = data.kcal.toFixed(0);
            // } else {
            //     return;
            // }

        }
    }
    // ACTION
const actions = {
    GET_RESULT: ({ commit, dispatch }, id) => {
        //让loading界面显示
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.getResult(id).then(response => {
                if (response.data.code == 200) {
                    commit(SET_RESULT, response.data.result.detail);
                } else {
                    commit(SET_RESULT, response.data.message);
                }
                // 关闭loading界面
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },
    //获取课程的数据结果
    GET_RESULT_BY: ({ commit, dispatch }, id) => {
        //让loading界面显示
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.getResultBY(id).then(response => {
            if (response.data.code === 200) {
                commit(SET_RESULT, response.data.result, response.data.message ? response.data.message : '');
            } else if (response.data.code === 201) {
                commit(SET_RESULT, response.data.message);
            }
        })
    }
}
export default {
    state,
    actions,
    mutations
}